﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class DAO_Phong
    {
        #region 1. Inserting
        public static bool ThemPhong(DTO_Phong p)
        {
            bool result = false;
            try
            {
                string sql;
                sql = @"INSERT INTO db_Phong VALUES  ('" + p.maPhong + "','" + p.tenPhong + "','" + p.LoaiPhong + "','" + p.tinhTrang + "')";
                RunQuery.RunNonQuery(sql);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Delete
        public static bool XoaPhong(DTO_Phong p)
        {
            bool result = false;
            try
            {
                string sql;
                sql = @"DELETE FROM db_Phong WHERE MaPhong = '" + p.maPhong + @"'";
                RunQuery.RunNonQuery(sql);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Retrieving

        public static List<DTO_Phong> LayTatCaPhong()
        {
            List<DTO_Phong> lp = new List<DTO_Phong>();
            try
            {
                string sql;
                sql = @"SELECT * FROM db_Phong";
                SqlDataAdapter a = new SqlDataAdapter();
                a = RunQuery.RunQueryData(sql);
                DataTable dt = new DataTable();
                a.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {
                    DTO_Phong p = new DTO_Phong();
                    p.maPhong = dr[0].ToString();
                    p.tenPhong = dr[1].ToString();
                    p.LoaiPhong = dr[2].ToString();
                    p.tinhTrang = dr[3].ToString();

                    lp.Add(p);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lp;
        }
        #endregion
    }
}
